﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AOP.Filter.Exception;
using AOP.Filter.Result;
using CacheService.Standard;
using DAL.Base;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;

namespace WebApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //过滤器
            services.AddMvc(c => {
                c.Filters.Add(typeof(AopExceptionFilterAttribute));
                c.Filters.Add(typeof(AopResultFilterAttribute));
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            //文档
            services.AddSwaggerGen(s => {
                s.SwaggerDoc("api", new Swashbuckle.AspNetCore.Swagger.Info
                {
                    Version = "1.0.0",
                    Title = "APP接口文档",
                    Description = ""
                });
                //描述文件
                var path = AppDomain.CurrentDomain.BaseDirectory;
                var xml = Path.Combine(path, "WebApp.xml");
                s.IncludeXmlComments(xml);
            });
            //数据库配置
            services.AddDbContext<ContextBase>(c => c.UseSqlServer(Configuration.GetConnectionString("database")));
            //所有接口注入
            services.AutoFacInterface();
            //缓存配置
            var cacheStr = Configuration.GetConnectionString("cache");
            if (string.IsNullOrWhiteSpace(cacheStr))
            {
                services.AddSingleton<ICache, MemoryForCache>();
            }
            else
            {
                services.AddSingleton(typeof(ICache), new RedisForCache(new RedisCacheOptions
                {
                    Configuration = cacheStr,
                    InstanceName = "api_name"
                }, 0));
            }
            return services.BuildServiceProvider();
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSwagger();
            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/api/swagger.json", "接口文档");
            });
            //静态资源使用目录
            string contentRoot = Directory.GetCurrentDirectory();
            app.UseStaticFiles();
            //跨域配置
            app.UseCors(c => {
                c.AllowAnyOrigin();
            });
            app.UseMvc();
        }
    }
}
